$(function(){
	getManagerName();
	init();
	$("#btnAdd").click(function(){
		var judge = true;
		for(var i = 0 ; i < _map.length;i++){
			var params={
					"bean.roleId":_map[i].roleId,
					"bean.roleJudge":_map[i].roleJudge,
					managerName:_managerName
			};
			$.post("/library-mannager/manageoperator/role/update.action",params,function(data){
				if(!data.message.state){
					judge = false;
				}
			});
		}
		if(judge){
			$.messager.show({  
                title:'message',  
                msg:"一键更新角色成功",  
                showType:'show'  
            });
		}else{
			$.messager.show({  
                title:'message',  
                msg:'一键更新角色失败',
                showType:'show'  
            });
		}
	});
});

var _selectData;
var _map = new Array();
var _managerName;
function getManagerName(){
	_managerName = parent._data;
}
function init() {
	$("#tab").datagrid({
		url:"/library-mannager/manageoperator/role/init.action?managerName="+_managerName,
		fitColumns : true,
		pagination : true,
		fit : true,
		rownumbers:true,
		pagination:true,
		onRowContextMenu : function(e, rowIndex, rowData) {
			 e.preventDefault(); //阻止浏览器捕获右键事件
			 $(this).datagrid("clearSelections"); //取消所有选中项
             $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
			$('#tmenu').menu('show', {
				left : e.pageX,
				top : e.pageY
			});
		},
		onSelect:function(rowIndex, rowData){
			_selectData = rowData;
		}
	});
	var p = $('#tab').datagrid('getPager');  
    $(p).pagination({  
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',  
    }); 
}
function checkchange(roleId){
	for(var i = 0 ; i < _map.length ;i++){
		if(roleId == _map[i].roleId){
			_map[i].roleJudge = !_map[i].roleJudge;
		}
	}
}

function formattercheck(value, rowData, rowIndex) {
	var newData = {"roleId":rowData.roleId,"roleJudge":rowData.roleJudge};
	_map.push(newData);
	var returnvalue;
	if (!value) {
		returnvalue = "<input type = 'checkbox' onclick = 'javascript:checkchange(\""+ rowData.roleId +"\")'>";
	} else {
		returnvalue = "<input type = 'checkbox' checked = 'checked' onclick = 'javascript:checkchange(\""+ rowData.roleId +"\")'>";
	}
	return returnvalue;
}